aboutsummaryrefslogtreecommitdiff
path: root/src/routes/projects/[id]
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-09-08 08:57:08 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-09-08 08:57:08 +0200
commit4b2b964283075712de98743720ff3e31370ddc8e (patch)
tree7200e83869dbd42d5f20318a07a6ca328e5ac59f /src/routes/projects/[id]
parent992e40835af1600a0961ec2454fb78ff43aec3c6 (diff)
Deduplicate routine actions and badges across project routes
- Share edit/complete/archive form actions in routine-actions.ts; route servers become thin wrappers (strictest guard semantics in one place) - Move parseFrequencyDays into validation.ts - Extract ScoreBadge/StatusBadge components; use on project pages and the home page instead of duplicated scoreClass/statusClass helpers - Fix archiveProject calling the routine archive with the project id
Diffstat (limited to 'src/routes/projects/[id]')
-rw-r--r--src/routes/projects/[id]/+page.server.ts63
-rw-r--r--src/routes/projects/[id]/+page.svelte27
2 files changed, 25 insertions, 65 deletions
diff --git a/src/routes/projects/[id]/+page.server.ts b/src/routes/projects/[id]/+page.server.ts
index a23d834..66cb9f3 100644
--- a/src/routes/projects/[id]/+page.server.ts
+++ b/src/routes/projects/[id]/+page.server.ts
@@ -1,26 +1,25 @@
import { error, fail, redirect } from '@sveltejs/kit';
-import { getById } from '$lib/server/services/projects';
+import {
+ archive as archiveProject,
+ getById,
+} from '$lib/server/services/projects';
import {
add,
- edit,
- archive,
- complete,
loadProjectDashboard,
loadArchived,
} from '$lib/server/services/routines';
-import { numericId, parseFormString } from '$lib/server/services/validation';
-import { parseIntervalToDays } from '$lib/shared/routine-metrics';
+import {
+ archiveRoutineAction,
+ completeRoutineAction,
+ editRoutineAction,
+} from '$lib/server/services/routine-actions';
+import {
+ numericId,
+ parseFormString,
+ parseFrequencyDays,
+} from '$lib/server/services/validation';
import type { Actions, PageServerLoad } from './$types';
-function parseFrequencyDays(value: FormDataEntryValue | null): number {
- const raw = value?.toString() ?? '';
- const days = parseIntervalToDays(raw);
- if (days === null) {
- throw new Error('Invalid interval');
- }
- return days;
-}
-
export const load: PageServerLoad = async ({ params }) => {
const id = numericId(params.id);
const project = await getById(id);
@@ -58,47 +57,21 @@ export const actions: Actions = {
editRoutine: async ({ request }) => {
const data = await request.formData();
- const name = parseFormString(data.get('name'));
- if (!name) {
- return fail(400, { error: 'Le nom est requis' });
- }
- try {
- await edit(numericId(data.get('id')), {
- name,
- frequencyDays: parseFrequencyDays(data.get('frequencyDays')),
- });
- } catch {
- return fail(400, { error: 'Modification invalide' });
- }
- return { success: true };
+ return editRoutineAction(data, numericId(data.get('id')));
},
complete: async ({ request }) => {
const data = await request.formData();
- try {
- await complete(
- numericId(data.get('id')),
- parseFormString(data.get('note')),
- new Date().toISOString(),
- );
- } catch {
- return fail(400, { error: 'Routine introuvable' });
- }
- return { success: true };
+ return completeRoutineAction(data, numericId(data.get('id')));
},
archiveRoutine: async ({ request }) => {
const data = await request.formData();
- try {
- await archive(numericId(data.get('id')));
- } catch {
- return fail(400, { error: 'Routine introuvable' });
- }
- return { success: true };
+ return archiveRoutineAction(numericId(data.get('id')));
},
archiveProject: async ({ params }) => {
- await archive(numericId(params.id));
+ await archiveProject(numericId(params.id));
throw redirect(303, '/');
},
};
diff --git a/src/routes/projects/[id]/+page.svelte b/src/routes/projects/[id]/+page.svelte
index 3d1aafe..e324f45 100644
--- a/src/routes/projects/[id]/+page.svelte
+++ b/src/routes/projects/[id]/+page.svelte
@@ -1,10 +1,11 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { matchItems } from '$lib/components/fuzzy-search';
+ import ScoreBadge from '$lib/components/ScoreBadge.svelte';
+ import StatusBadge from '$lib/components/StatusBadge.svelte';
import {
formatFrequency,
parseIntervalToDays,
- STATUS_LABELS,
type RoutineState,
} from '$lib/shared/routine-metrics';
@@ -44,25 +45,12 @@
editName = routine.name;
editFrequency = `${routine.frequencyDays}d`;
}
-
- function scoreClass(score: number): string {
- if (score >= 80) return 'score bg-success';
- if (score >= 50) return 'score bg-accent';
- return 'score bg-danger';
- }
-
- function statusClass(status: RoutineState['status']): string {
- if (status === 'ok') return 'status bg-success text-white';
- if (status === 'due') return 'status bg-accent-soft text-accent';
- if (status === 'late') return 'status bg-accent text-white';
- return 'status bg-danger text-white';
- }
</script>
<main>
<header class="page-header">
<h1>{data.project.name}</h1>
- <span class={scoreClass(data.healthScore)}>{data.healthScore}</span>
+ <ScoreBadge score={data.healthScore} />
</header>
<form
@@ -183,11 +171,10 @@
{/if}
</span>
</div>
- <span class={statusClass(routine.status)}>
- {STATUS_LABELS[routine.status]}
- {#if routine.overdueDays > 0}({routine.overdueDays}
- j){/if}
- </span>
+ <StatusBadge
+ status={routine.status}
+ overdueDays={routine.overdueDays}
+ />
<form method="POST" action="?/complete" use:enhance>
<input type="hidden" name="id" value={routine.id} />
<button class="btn-done" type="submit">Fait</button>